package com.yin.myproject.spring.core.util;

import com.yin.myproject.spring.beans.BeanInstantiationException;

/**
 * Created by Eason on 2017/9/18 0018.
 */
public class BeanUtils {

    /**
     * 使用无参构造函数实例化一个类
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T instantiateClass(Class<T> clazz){
        if (clazz.isInterface()) {
            throw new BeanInstantiationException(clazz, "Specified class is an interface");
        }
        try {
            return clazz.newInstance();
        }catch (InstantiationException ex) {
            throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
        }
        catch (IllegalAccessException ex) {
            throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
        }
    }
}
